package com.canal.admin.service.impl;

import com.canal.admin.config.properties.CanalProperties;
import com.canal.admin.dao.ZcolChannelMapper;
import com.canal.admin.entity.canal.ZcolChannel;
import com.canal.admin.exception.BuzException;
import com.canal.admin.service.ZcolChannelService;
import com.canal.admin.transfer.req.channel.ChannelAddReq;
import com.canal.admin.transfer.req.channel.ChannelCondition;
import com.canal.admin.transfer.req.channel.ChannelUpReq;
import com.canal.admin.transfer.resp.PageResp;
import com.canal.admin.transfer.resp.canal.ChannelListResp;
import com.canal.admin.transfer.resp.canal.ChannelResp;
import com.canal.admin.util.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 渠道管理
 * @author yangbo
 * @date 2020-06-23 14:38
 */
@Service
@Slf4j
public class ZcolChannelServiceImpl implements ZcolChannelService {

    @Autowired
    private ZcolChannelMapper channelMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private CanalProperties canalProperties;

    @Override
    public PageResp<ChannelListResp> channelList(ChannelCondition condition) {
        PageHelper.startPage(condition.getPageNo(),condition.getPageSize());
        List<ChannelListResp> channelListRespList = channelMapper.findChannelByConditions(condition.getKeyword());
        PageInfo<ChannelListResp> pageInfo = new PageInfo<>(channelListRespList);
        return PageUtil.toResp(pageInfo);
    }

    /**
     * 新增渠道
     * @param addReq
     * @throws BuzException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addChannel(ChannelAddReq addReq) throws BuzException {
        //存在校验
        if(channelMapper.existName(addReq.getName()) > 0) {
            throw new BuzException(500,"渠道名已存在");
        }
        if(channelMapper.existBuzName(addReq.getBuzName()) > 0) {
            throw new BuzException(500,"渠道账号名已存在");
        }
        //组装数据
        ZcolChannel channel = new ZcolChannel();
        channel.setName(addReq.getName());
        channel.setBuzName(addReq.getBuzName());
        channel.setBuzPassword(passwordEncoder.encode(addReq.getBuzPassword()));
        channelMapper.addChannel(channel);
    }

    /**
     * 删除
     * @param id
     * @throws BuzException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delChannel(Integer id) throws BuzException {
        if(channelMapper.existChannelItem(id) > 0) {
            throw new BuzException(500,"渠道下存在渠道号，无法删除");
        }
        channelMapper.delById(id);
    }

    /**
     * 明细
     * @param id
     * @return
     * @throws BuzException
     */
    @Override
    public ChannelResp detail(Integer id) {
        return channelMapper.detailById(id);
    }

    /**
     * 编辑
     * @param upReq
     * @throws BuzException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ChannelUpReq upReq) throws BuzException {
        //存在校验
        if(channelMapper.existNameAndId(upReq.getName(),upReq.getId()) > 0) {
            throw new BuzException(500,"渠道名已存在");
        }
        if(channelMapper.existBuzNameAndId(upReq.getBuzName(),upReq.getId()) > 0) {
            throw new BuzException(500,"渠道账号名已存在");
        }
        //组装数据
        ZcolChannel channel = new ZcolChannel();
        channel.setId(upReq.getId());
        channel.setName(upReq.getName());
        channel.setBuzName(upReq.getBuzName());
        channelMapper.upChannel(channel);
    }

    /**
     * 重置密码
     * @param id
     * @throws BuzException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void resetPwd(Integer id) throws BuzException {
        //组装数据
        ZcolChannel channel = new ZcolChannel();
        channel.setId(id);
        channel.setBuzPassword(passwordEncoder.encode(canalProperties.getBuzPassword()));
        channelMapper.upBuzPassword(channel);
    }
}
